/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NicholasValueBeans;

import NicholasDatabase.SQLqueries;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author ChuckToh
 */
public class VoteTopics {

    private String topicsDelimiter = ",";
    private String[] topics;
    private int voteListId;
    private Date dateCreated, dateStart, dateEnd;

    public VoteTopics() {
        setVoteTopics();
    }

    private void setVoteTopics() {
        try {
            Calendar cal = Calendar.getInstance();
            String time = String.format("%02d", cal.get(Calendar.YEAR)) + "-" + String.format("%02d", cal.get(Calendar.MONTH) + 1) + "-" + String.format("%02d", cal.get(Calendar.DAY_OF_MONTH));
            Date date = convertStrToDate(time);
            SQLqueries sql = new SQLqueries();
            ResultSet voteTopicsRow = sql.voteTopicsQuery(date);

            if (voteTopicsRow.next()) {
                this.voteListId = voteTopicsRow.getInt("id");
                String tempTopics = voteTopicsRow.getString("topics");
                topics = tempTopics.split(topicsDelimiter);
                this.dateCreated = voteTopicsRow.getDate("dateCreated");
                this.dateStart = voteTopicsRow.getDate("dateStart");
                this.dateEnd = voteTopicsRow.getDate("dateEnd");
            } else {
                this.voteListId = 0;
                this.topics = null;
                this.dateCreated = null;
                this.dateStart = null;
                this.dateEnd = null;
            }
            voteTopicsRow.close();
            sql.closeQuery();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public String[] getVoteTopics() {
        return this.topics;
    }

    public int getVoteListId() {
        return this.voteListId;
    }

    public Date getDateCreated() {
        return this.dateCreated;
    }

    public Date getDateStart() {
        return this.dateStart;
    }

    public Date getDateEnd() {
        return this.dateEnd;
    }

    private Date convertStrToDate(String dateStr) {
        Date date = null;

        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date tempDate = df.parse(dateStr);
            date = new Date(tempDate.getTime());
        } catch (ParseException ex) {
            System.out.println(ex.getMessage());
        }

        return date;
    }
}
